/*
 * @Author: ZhangZheyi
 * @Date:   2016-05-19 11:46:57
 * @Last Modified by:   ZhangZheyi
 * @Last Modified time: 2016-05-19 19:36:19
 */

'use strict';

// 引入 gulp
var gulp = require('gulp');
var gutil = require('gulp-util');

var webpack = require("webpack");
var webpackConfig = require("./webpack.config.js");
var child_process = require('child_process');  //调用子进程
// 引入组件
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');


// 编译Sass
gulp.task('sass', function() {
    gulp.src('./public/sass/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('./public/css'));
});

// // 合并，压缩文件
// gulp.task('scripts', function() {
//     gulp.src('./js/*.js')
//         .pipe(concat('all.js'))
//         .pipe(gulp.dest('./dist'))
//         .pipe(rename('all.min.js'))
//         .pipe(uglify())
//         .pipe(gulp.dest('./dist'));
// });

gulp.task('default', function() {
    console.log('Nothing happened!');
    console.log('Done!');
});


gulp.task("webpack", function(callback) {
    // run webpack
    var myConfig = Object.create(webpackConfig);
    // run webpack
    webpack(
        // configuration
        myConfig,
        function(err, stats) {
            if(err) throw new gutil.PluginError("webpack", err);
            gutil.log("[webpack]", stats.toString({
              // output options
            }));
            callback();
        });
});

gulp.task('server', function() {
    // gulp.run( 'sass', 'scripts');
    var child = child_process.fork('app2.js');
    gulp.run('sass');
    gulp.watch('./public/javascripts/*.js', ['webpack']);
//nodemon brower-sync

});
